class Local {
    constructor(key) {
        this.key = key;
        this.initLo();
    }
    setData(key, value) { // 保存键值对到本地
        if (typeof value === 'undefined') {
            return localStorage.setItem(this.key, JSON.stringify(key));
        }
        if (typeof value === 'string') {
            return localStorage.setItem(key, value);
        }
        try {
            localStorage.setItem(key, JSON.stringify(value));
        } catch (err) {
            return err;
        }
    }
    getData(key) { // 获取指定key的数据
        const value = localStorage.getItem(key || this.key);
        try {
            return JSON.parse(value);
        } catch (err) {
            return value;
        }
    }
    removeData(key) { // 删除指定key的数据
        return localStorage.removeItem(key);
    }
    clearData() { // 删除所有的本地数据
        return localStorage.clear();
    }
    initLo() { // 初始化
        if (!window.localStorage) {
            throw new Error("浏览器不支持 localstorage 本地存储！！！")
        }
    }
}

(function local(local = 'local') {
    var local_ = local;
    if (window[local]) return console.log('属性名重复！！');
    try {
        window[local_] = new Local(local);
        console.log('封装本地存储成功，可以直接使用 ' + local_ + ' 对象来操作本地存储数据！')
    } catch (err) {
        console.log(err)
        return err;
    }
})("db")


if (!db.getData('start')) return;


let forTime = setInterval(() => {
    fetch('https://www.qidian.com/').then((res) => {
        return res.text();
    }).then((res) => {
        window.aa = $(res)
        // debugger
        let tr = aa.find('.update-table.all tr');
        let dbData = db.getData();
        for (let index = 0; index < tr.length; index++) {
            // td = $(tr[index]).find('td');
            let td = tr[index].querySelectorAll('td');
            let content = {
                type: td[0].innerText.trim(),
                name: td[1].innerText.trim(),
                href: td[1].querySelector('a').href,
                node: td[2].innerText.trim(),
                author: td[3].innerText.trim(),
            }
            dbData[content.name] = content;
        }
        db.setData(dbData);
        console.log('搜索结果：', dbData)
    })
}, 1000 * 30);

function created() {
    let html = `
    <div onclick="init()"
        style="position: fixed; display: block; height: 20px; width: 20px; top: 50%; z-index: 100; background-color: #e9e9e9; padding: 20px; box-sizing: border-box;">
        打开</div>
    `
    $(document.body).append(html)
}

created()